package esa.isoc.statistics;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class StatisticFile {
	public final int revolutionNumber;
	public final int version;
	public final File file;
	public final Map<String, Entry> mapEntries = new HashMap<String, Entry>();
	public StatisticFile(int revolutionNumber, int version) {
		this.revolutionNumber = revolutionNumber;
		this.version = version;
		this.file = createFile();
	}
	
	public StatisticFile(String fileName) {
		fileName = fileName.substring(0, fileName.indexOf("."));
		int position = fileName.indexOf("_");
		if (position > -1) {
			this.revolutionNumber = Integer.parseInt(fileName.substring(0, position));
			this.version = Integer.parseInt(fileName.substring(position + 1));
		} else {
			this.revolutionNumber = Integer.parseInt(fileName);
			this.version = 0;
		}
		this.file = createFile();

	}
	
	private File createFile() {
		StringBuilder sb = new StringBuilder();
 			
		sb.append(getRevolutionNumber());

		if (version > 0) {
			sb.append("_");
			sb.append(String.format("%02d", version));
		}
		sb.append(".STATS");	
		return new File(FileManager.DIRECTORY, sb.toString());
		
	}
	
	public String getRevolutionNumber() {
		return String.format("%04d", revolutionNumber);
	}
	

	public boolean isNewerThan(StatisticFile file) {
		return this.version > file.version;
	}
	
	
	
	public File getFile() {
		return file;
	}
	
	public void readFile() {

		try {
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(file);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String strLine;
			// Read File Line By Line
			while ((strLine = br.readLine()) != null) {
				// Print the content on the console
				System.out.println(strLine);
			}
			// Close the input stream
			in.close();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}
	
	public void readFileData() {

		try {
			// Open the file that is the first
			// command line parameter
			FileInputStream fstream = new FileInputStream(file);
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
		
			// the first line is useless
			
			String header = br.readLine();
			String strLine;
			if (revolutionNumber == 27) {
				System.out.println("test");
			}
			while ((strLine = br.readLine()) != null) {
				// Print the content on the console
				
					if (strLine.length()==0) continue;
					strLine = strLine.replaceAll("\\\t\\\t", "\t0\t");
					strLine = strLine.replaceAll("\\\t \\\t", "\t0\t");
					StringTokenizer st = new StringTokenizer(strLine, "\t");
					if (st.hasMoreElements() == false) continue;
					String key = st.nextToken();
					if (StatisticsRow.isValidRow(key) == false) {
						if (key.startsWith("Open time grade 1")){
							key = key.replaceFirst("Open time grade 1", StatisticsRow.OPEN_TIME_GR_A.description);
						} else if (key.startsWith("Open time grade 2")){
							key = key.replaceFirst("Open time grade 2", StatisticsRow.OPEN_TIME_GR_B.description);
						} else if (key.startsWith("Open time grade 3")){
							key = key.replaceFirst("Open time grade 3", StatisticsRow.OPEN_TIME_GR_C.description);
						} else if (key.startsWith("TOO")){
							key = key.replaceFirst("TOO", StatisticsRow.TOTAL_TOO.description);							
						} else {
							continue;
						}
					}
					key = key.substring(0, key.indexOf(":"));
					key =key.trim();
					Entry  entry;
					if (header.contains("Total Hours") == false) {
						entry = new NewEntry(st);
					} else {
						entry = new OldEntry(st);
					}
					mapEntries.put(key, entry);
				 

				
			}
			// Close the input stream
			in.close();
			
			
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + revolutionNumber;
		result = prime * result + version;
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		StatisticFile other = (StatisticFile) obj;
		if (revolutionNumber != other.revolutionNumber)
			return false;
		if (version != other.version)
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		return "StatisticFile [revolutionNumber=" + revolutionNumber
				+ ", version=" + version + "]";
	}
	
	
	
}
